#include "MainMenu.h"

MainMenu::MainMenu(void)
{
	// Setting size 
	Background = Object2D(800.0, 600.0, 0.0, false);
	Button_1P = Object2D(150.0, 50.0, 50.0, true);
	Button_2P = Object2D(150.0, 50.0, 50.0, true);
	Button_Option = Object2D(150.0, 50.0, 50.0, true);
	Button_HighScore = Object2D(150.0, 50.0, 50.0, true);
	Button_Quit = Object2D(150.0, 50.0, 50.0, true);
	MouseCursor= Object2D(25.0, 25.0, 25.0, true);

	IsSplash = true;
	SplashTime = TimeCount;
	DelayTime = Delay;
	SplashWord = 1.0f;

	// Controlling button animation
	// 0 = button not selected
	// 1 = button selected
	// 2 = button pressed
	Button1P_Index = 0;
	Button2P_Index = 0;
	ButtonOption_Index = 0;
	ButtonHighScore_Index = 0;
	ButtonQuit_Index = 0;

	// Setting position
	Background.setPosition(400.0, 300.0);
	Button_1P.setPosition(250,250);
	Button_2P.setPosition(550,250);
	Button_Option.setPosition(400.0, 350.0);
	Button_HighScore.setPosition(400.0, 450.0);
	Button_Quit.setPosition(400.0, 550.0);	
}

void MainMenu::reset()
{
	SplashWord = 1.0f;
	IsSplash = true;
	SplashTime = TimeCount;
	DelayTime = Delay;

	Button1P_Index = 0;
	Button2P_Index = 0;
	ButtonOption_Index = 0;
	ButtonHighScore_Index = 0;
	ButtonQuit_Index = 0;
}

void MainMenu::freeResource()
{
	freeTexture(&TextureMenu);

	for(int x = 0; x < 3; x++)
		freeTexture(&Texture1P[x]);

	for(int x = 0; x < 3; x++)
		freeTexture(&Texture2P[x]);

	for(int x = 0; x < 3; x++)
		freeTexture(&TextureOption[x]);

	for(int x = 0; x < 3; x++)
		freeTexture(&TextureHighScore[x]);

	for(int x = 0; x < 3; x++)
		freeTexture(&TextureQuit[x]);
}

void MainMenu::loadResource()
{
	// Background Menu
	LoadTGA(&TextureMenu, "images/mainmenu/Image_MainMenu.tga", false);

	// Button 1P
	LoadTGA(&Texture1P[0], "images/mainmenu/Button_1Player_Off.tga", false);
	LoadTGA(&Texture1P[1], "images/mainmenu/Button_1Player_On.tga", false);
	LoadTGA(&Texture1P[2], "images/mainmenu/Button_1Player_Pressed.tga", false);

	// Button 2P
	LoadTGA(&Texture2P[0], "images/mainmenu/Button_2Player_Off.tga", false);
	LoadTGA(&Texture2P[1], "images/mainmenu/Button_2Player_On.tga", false);
	LoadTGA(&Texture2P[2], "images/mainmenu/Button_2Player_Pressed.tga", false);

	// Button Option
	LoadTGA(&TextureOption[0], "images/mainmenu/Button_Option_Off.tga", false);
	LoadTGA(&TextureOption[1], "images/mainmenu/Button_Option_On.tga", false);
	LoadTGA(&TextureOption[2], "images/mainmenu/Button_Option_Pressed.tga", false);

	// Button HighScore
	LoadTGA(&TextureHighScore[0], "images/mainmenu/Button_HighScore_Off.tga", false);
	LoadTGA(&TextureHighScore[1], "images/mainmenu/Button_HighScore_On.tga", false);
	LoadTGA(&TextureHighScore[2], "images/mainmenu/Button_HighScore_Pressed.tga", false);

	// Button Quit
	LoadTGA(&TextureQuit[0], "images/mainmenu/Button_Quit_Off.tga", false);
	LoadTGA(&TextureQuit[1], "images/mainmenu/Button_Quit_On.tga", false);
	LoadTGA(&TextureQuit[2], "images/mainmenu/Button_Quit_Pressed.tga", false);
}

void MainMenu::draw()
{
	// Splash
	if(IsSplash == true)
	{
		SplashWord -= 0.0005;				
		glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
		glColor4f(1.0, 1.0, 1.0, SplashWord);														// By changing the temp value to create a fading effect
		glEnable(GL_BLEND);
		printw(350.0, 250.0, 0.0, "Product By");
		printw(300.0, 320.0, 0.0, "Team NOBUNAGA");
		glDisable(GL_BLEND);
	}

	// Main Menu
	else
	{
		Background.drawPlane(TextureMenu.texID);

		Button_1P.drawPlane(Texture1P[Button1P_Index].texID);
		Button_2P.drawPlane(Texture2P[Button2P_Index].texID);
		Button_Option.drawPlane(TextureOption[ButtonOption_Index].texID);
		Button_HighScore.drawPlane(TextureHighScore[ButtonHighScore_Index].texID);
		Button_Quit.drawPlane(TextureQuit[ButtonQuit_Index].texID);
	}
}

void MainMenu::update()
{
	// Splash
	if(SplashTime != 0)
	{
		// Update splash time
		float TimePassed = RealTimeController::getInstance().getTimeInterval();
		if(TimePassed > 1000)
			SplashTime -= 1;

		if(SplashTime < 0)
		{
			SplashTime = 0;
		}
	}

	// Change to main menu page
	if(SplashTime == 0)
	{
		IsSplash = false;
	}

	// Enter 1 player game mode
	if(Button1P_Index == 2)
	{
		// Update delay time
		float TimePassed = 1 / RealTimeController::getInstance().getFPS();
		DelayTime -= TimePassed;

		if(DelayTime < 0)
		{
			// Goes into stage info page
			cout << "Enter Stage info Page" << endl;
	
			SceneManager::getInstance().changeScene("Stage_Info");
		}
	}

	// Enter 2 player game mode
	if(Button2P_Index == 2)
	{
		// Update delay time
		float TimePassed = RealTimeController::getInstance().getTimeInterval();
		if(TimePassed > 1000)
			DelayTime -= 1;

		if(DelayTime < 0)
		{
			// Goes into stage info page
			cout << "Enter Stage info Page" << endl;
			SceneManager::getInstance().changeScene("Stage_Info");
		}
	}

	// Enter option page
	if(ButtonOption_Index == 2)
	{
		// Update delay time
		float TimePassed = RealTimeController::getInstance().getTimeInterval();
		if(TimePassed > 1000)
			DelayTime -= 1;

		if(DelayTime < 0)
		{
			// Goes into option page
			cout << "Enter Option Page" << endl;
		}
	}

	// Enter highscore page
	if(ButtonHighScore_Index == 2)
	{
		// Update delay time
		float TimePassed = RealTimeController::getInstance().getTimeInterval();
		if(TimePassed > 1000)
			DelayTime -= 1;

		if(DelayTime < 0)
		{
			// Goes into  highscore page
			cout << "Enter highscore Page" << endl;
			SceneManager::getInstance().changeScene("High_Score");
		}
	}

	// Quit game
	if(ButtonQuit_Index == 2)
	{
		// Update delay time
		float TimePassed = RealTimeController::getInstance().getTimeInterval();
		if(TimePassed > 1000)
			DelayTime -= 1;

		if(DelayTime < 0)
		{
			glutLeaveMainLoop();
		}
	}
}

void MainMenu::handleKeyboard(int key, int x, int y)
{
}

void MainMenu::handleKeyboardUp(int key, int x, int y)
{
}

void MainMenu::handleKeyboardSpecial(int key, int x, int y)
{
}

void MainMenu::getMid(int &x, int &y)
{
}

void MainMenu::handleMouseClick(int button, int state, int x, int y)
{
	// Update mousecursor position
	MouseCursor.setPosition(x,600 - y);

	// Button 1P pressed
	if(Button_1P.Intersects(MouseCursor)) 
	{
		_SinglePlayer = true;
		Button1P_Index = 2;
	}

	// Button 2P pressed
	if(Button_2P.Intersects(MouseCursor)) 
	{
		_SinglePlayer = false;
		Button2P_Index = 2;
	}

	// Button Option pressed
	if(Button_Option.Intersects(MouseCursor)) 
	{
		ButtonOption_Index = 2;
	}

	// Button HighScore pressed
	if(Button_HighScore.Intersects(MouseCursor)) 
	{
		ButtonHighScore_Index = 2;
	}

	// Quit game
	if(Button_Quit.Intersects(MouseCursor)) 
	{
		ButtonQuit_Index = 2;
	}
}

void MainMenu::handleMouseMove(int x, int y)
{
	// Update mousecursor position
	MouseCursor.setPosition(x,600 - y);

	// If button not pressed
	if(Button1P_Index != 2)
	{
		if(Button_1P.Intersects(MouseCursor))
		{
			Button1P_Index = 1;		// Button 1P light on
		}
		else
		{
			Button1P_Index = 0;		// Button 1P light off
		}
	}

	// If button not pressed
	if(Button2P_Index != 2)
	{
		if(Button_2P.Intersects(MouseCursor)) 
		{
			Button2P_Index = 1;		// Button 2P light on
		}
		else
		{
			Button2P_Index = 0;		// Button 2P light off
		}
	}

	// If button not pressed
	if(ButtonOption_Index != 2)
	{
		if(Button_Option.Intersects(MouseCursor)) 
		{
			ButtonOption_Index = 1;		// Button option light on
		}
		else
		{
			ButtonOption_Index = 0;		// Button option light off
		}
	}

	// If button not pressed
	if(ButtonHighScore_Index != 2)
	{
		if(Button_HighScore.Intersects(MouseCursor)) 
		{
			ButtonHighScore_Index = 1;		// Button highscore light on
		}
		else
		{
			ButtonHighScore_Index = 0;		// Button highscore light off
		}
	}

	// If button not pressed
	if(ButtonQuit_Index != 2)
	{
		if(Button_Quit.Intersects(MouseCursor)) 
		{
			ButtonQuit_Index = 1;		// Button Quit light on
		}
		else
		{
			ButtonQuit_Index = 0;		// Button Quit light off
		}
	}
}